package com.lumm.services.impl;

import com.lumm.dao.DetailsMapper;
import com.lumm.dao.UserMapper;
import com.lumm.model.*;
import com.lumm.services.RecommendService;
import com.lumm.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("RecommendServiceImpl")
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DetailsMapper detailsMapper;

    private UserExample userExample = new UserExample();
    private DetailsExample detailsExample=new DetailsExample();

    @Override
    public List<RecommendItem> getRecommendList(User user) {

        String gender=user.getGender();

        RecommendItem recommendItem;

        userExample.clear();
        detailsExample.clear();

        userExample.setOrderByClause("rand() limit 30");

        userExample.or().andGenderNotEqualTo(gender);

        List<User> users=userMapper.selectByExampleWithBLOBs(userExample);


        List<RecommendItem> recommendItems=new ArrayList<>();
        for (User oneUser:users) {

            DetailsWithBLOBs details=detailsMapper.selectByPrimaryKey(oneUser.getUserId());
            recommendItem=new RecommendItem();
            recommendItem.setUserId(oneUser.getUserId());
            recommendItem.setGender(oneUser.getGender());
            recommendItem.setSelfIntro(oneUser.getSelfIntro());
            recommendItem.setVipLevel(oneUser.getVipLevel());
            recommendItem.setName(oneUser.getName());


            try {
                recommendItem.setAge(DateUtil.getAge(oneUser.getBirthday()));
            } catch (Exception e) {
                e.printStackTrace();
                recommendItem.setAge(0);
            }

            if (details!=null){

                recommendItem.setDomicile(details.getDomicile());
                recommendItem.setHeight(details.getHeight());
                recommendItem.setEducation(details.getEducation());
                recommendItem.setEducation(details.getEducation());
                recommendItem.setMonthlySalary(details.getMonthlySalary());
                recommendItem.setWeight(details.getWeight());
                recommendItem.setHobby(details.getHobby());
                recommendItem.setLoveDeclaration(details.getLoveDeclaration());
                recommendItem.setDomicile(details.getDomicile());

            }

            recommendItems.add(recommendItem);
        }
        return recommendItems;
    }
}
